﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace nTribeHR.Model
{
    public enum TribeHRUserRole
    {
        Administrators = 1,
        Managers = 2,
        Employees =3
    }

    public enum TribeHRUserAccountState
    {
        Active = 0,
        Suspended = 1,
        Terminiated = 2
    }

    public class TribeUser : TribeHRBase
    {
        public int Id { get; set; }
        public TribeHRUserRole Group_Id { get; set; }
        public TribeHRUserAccountState Access { get; set; }
        public string Group_Name { get; private set; }
        public string Username { get; private set; }
        public string Full_Name { get; private set; }
        public string Email { get; set; }
        public string Twitter_Username { get; set; }
        public string LinkedIn_Url { get; set; }
        public string Blog_Url { get; set; }
        public int? Incomplete_Goal_Count { get; set; }
        public int Skills_Count { get; private set; }
        public int Notes_Received { get; private set; }
        public int Kudos_Received { get; private set; }
        public int Values_Supported { get; private set; }
        public DateTime? Created { get; set; }

        public DateTime? Modified { get; set; }

        public static TribeUser Create(XmlNode user)
        {
            if(user.Attributes == null)
            {
                throw new ArgumentNullException("user", "The attributes of the given XMLNode are missing.");
            }
            var tribeUser = new TribeUser();
            
            tribeUser.Id = GetAttributeInt(user, "id");
            
            TribeHRUserRole group;
            if(Enum.TryParse(GetAttributeString(user, "group_id"), out group))
            {
                tribeUser.Group_Id = @group;
            }
            tribeUser.Group_Name = GetAttributeString(user, "group_name");

            TribeHRUserAccountState accountState;
            if(Enum.TryParse(GetAttributeString(user,"access"), out accountState))
            {
                tribeUser.Access = accountState;
            }
            
            tribeUser.Username = GetAttributeString(user, "username");
            
            tribeUser.Full_Name = GetAttributeString(user, "full_name");

            tribeUser.Email = GetAttributeString(user, "email");

            tribeUser.Twitter_Username = GetAttributeString(user, "twitter_username");

            tribeUser.Blog_Url = GetAttributeString(user, "blog_url");

            tribeUser.Incomplete_Goal_Count = GetAttributeInt(user, "incomplete_goal_count");

            tribeUser.Skills_Count = GetAttributeInt(user, "skills_count");

            tribeUser.Notes_Received = GetAttributeInt(user, "notes_received");

            tribeUser.Kudos_Received = GetAttributeInt(user, "kudos_received");
            
            tribeUser.Values_Supported = GetAttributeInt(user, "values_supported");

            tribeUser.Created = GetAttributeDate(user, "created");

            tribeUser.Modified = GetAttributeDate(user, "modified");

            return tribeUser;
        }
    }

    public class TribeHRBase
    {
        public static string GetAttributeString(XmlNode node, string attributeName)
        {
            return node == null || 
                node.Attributes == null ||
                node.Attributes.GetNamedItem(attributeName) == null
                       ? String.Empty
                       : node.Attributes.GetNamedItem(attributeName).Value;
        }

        public  static int GetAttributeInt(XmlNode node, string attributeName)
        {
            int result = 0;
            if( node == null || 
                node.Attributes == null ||
                node.Attributes.GetNamedItem(attributeName) == null)
            {
                return result;
            }
            if(!Int32.TryParse(node.Attributes.GetNamedItem(attributeName).Value, out result))
            {
                throw new Exception(string.Format("Unable to parse ({0}) to and integer", 
                    node.Attributes.GetNamedItem(attributeName).Value));
            }
            return result;
        }

        public static DateTime? GetAttributeDate(XmlNode node, string attributeName)
        {
            DateTime? result = null;
            if( node == null ||
                node.Attributes == null ||
                node.Attributes.GetNamedItem(attributeName) == null
                )
            {
                return result;
            }

            DateTime parsedDate;
            if(DateTime.TryParse(node.Attributes.GetNamedItem(attributeName).Value, out parsedDate))
            {
                result = parsedDate;
            }
            return result;
        }
    }
}
